var request = require('request-promise');
const sha1 = require('sha1')
const { setQuery } = require('../common/common')
const { H5 } = require('./variables.js');
const { appid, secret, grant_type2, noncestr, game_url } = H5;
let grant_type = grant_type2;
const getJsApi = async (ctx, next) => {
    let queryString1 = setQuery({ appid, secret, grant_type });
    const opt1 = {
        method: 'GET',
        json: true,
        url: 'https://api.weixin.qq.com/cgi-bin/token?' + queryString1
    };
    let result1 = await request(opt1);
    const opt2 = {
        method: 'GET',
        json: true,
        url: 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access\_token=' + result1.access_token + '&type=jsapi'
    };
    let result2 = await request(opt2);
    let timestamp = Math.floor(Date.now() / 1000);
    var string = 'jsapi_ticket=' + result2.ticket + '&noncestr=' + noncestr + '&timestamp=' + timestamp + '&url=' + game_url;
    var signature = sha1(string);
    return {
        success: 1,
        content: {
            appId: appid,
            timestamp,
            nonceStr: noncestr,
            signature
        },
    }
}
module.exports = getJsApi;